package org.springside.modules.web.struts2;

import java.util.List;

import org.apache.commons.beanutils.MethodUtils;

/**
 * Manager的CRUD方法调用者的默认实现.
 * 方法命名规则为createXXX(),XXX为entity名,如createUser(),以便一个Manager里可以同时管理该模块的多种对象.
 */
public class DefaultCRUDInvoker implements CRUDInvoker {

	/**
	 * 实际代码如: <code>return userManager.getAllUser();</code>
	 */
	@SuppressWarnings("unchecked")
	public List getAll(final Object manager, final String entityClassName,
			final int[] rowStartIdxAndCount) throws Exception {
		return (List) MethodUtils.invokeMethod(manager, "getAll"
				+ entityClassName, rowStartIdxAndCount);
	}
 
	/**
	 * 实际代码如: <code>userManager.createUser(entity);</code>
	 */
	@SuppressWarnings("unchecked")
	public void create(final Object manager, final Class entityClass,
			final Object entity) throws Exception {
		MethodUtils.invokeMethod(manager, "create"
				+ entityClass.getSimpleName(), new Object[] { entity },
				new Class[] { entityClass });
	}

	/**
	 * 实际代码如: <code>userManager.updateUser(user);</code>
	 */
	@SuppressWarnings("unchecked")
	public void update(final Object manager, final Class entityClass,
			final Object entity) throws Exception {
		MethodUtils.invokeMethod(manager, "update"
				+ entityClass.getSimpleName(), new Object[] { entity },
				new Class[] { entityClass });
	}

	/**
	 * 实际代码如: <code>userManager.deleteUser(id);</code>
	 */
	public void delete(final Object manager, final String entityClassName,
			final Object id) throws Exception {
		MethodUtils.invokeMethod(manager, "delete" + entityClassName, id);
	}

}
